import axios from 'axios'
import { Toast } from 'vant'
import store from '@/store'
const axios1 = axios.create({
  baseURL: 'https://smart-shop.itheima.net/index.php?s=/api',
  timeout: 5000
})
axios1.interceptors.request.use(
  config => {
    (
      Toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0
      }))
    const token = store.state.user.userInfo.token
    if (token) {
      config.headers['Access-Token'] = token
      config.headers.platform = 'H5'
    }

    return config
  },
  error => {
    // 对请求错误做些什么
    return Promise.reject(error)
  }
)
axios1.interceptors.response.use(
  response => {
    if (response.data.status !== 200) {
      // 如果响应状态码不是200，提示错误信息
      Toast.fail(response.data.message || '请求失败')
      return Promise.reject(new Error(response.data.message || 'Error'))
    } else {
      Toast.clear()
      return response.data
    }
  },
  error => {
    // 对响应错误做点什么
    return Promise.reject(error)
  }
)
export default axios1
